package yambal.net.google.Spreadsheet
{
	import com.adobe.utils.DateUtil;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
	
	import mx.collections.ArrayCollection;
	
	public class Sheet
	{
		public var spreadsheet:Spreadsheets
		
		public var id:String;
		public var updated:Date;
		public var title:String;
		public var _content_url:String;
		
		public var WorkSheets:ArrayCollection;
		
		public function Sheet()
		{
			this.busy_child = new Array();
		}
		
		// ----------------------------------------------------------
		/**
		 * busyコントロール
		 **/
		private var _isBusy:Boolean = false;
		private function isBusy(f:Boolean):void{
			if(this._isBusy != f){
				this._isBusy = f;
				this.spreadsheet.childIsBusy(this._isBusy, this.id);
			}
		}
		
		// 自分自身のBusy
		private var _iAmBusy:Boolean = false;
		private function iAmBusy(f:Boolean):void{
			if(this._iAmBusy != f){
				this._iAmBusy = f;
			}
			
			if(!this.ChildIsBusy && !this._iAmBusy){
				this.isBusy(false);
			}else{
				this.isBusy(true);
			}
		}
		
		// 子のBusy(子から呼び出し)
		private var busy_child:Array;
		private var ChildIsBusy:Boolean = false;
		public function childIsBusy(f:Boolean, id:String):void{
			var test:int = this.busy_child.indexOf(id);
			if(test==-1 && f){
				this.busy_child.push(id);
				
			}else if(test!=-1 && !f){
				var h:Array = new Array();
				if(test != 0){
					h = this.busy_child.slice(0, test-1);
				}
				var b:Array = this.busy_child.slice(test+1);
				this.busy_child = h.concat(b);
				
			}
			this.ChildIsBusy = this.busy_child.length != 0
			
			if(!this.ChildIsBusy && !this._iAmBusy){
				this.isBusy(false);
			}else{
				this.isBusy(true);
			}
		}
		
		// ----------------------------------------------------------
		public function toString():String{
			var ret:String = "id:" + this.id + ", " + 
					"updated:" + this.updated + ", " + 
					"title:" + this.title + ", " + 
					"content_url:" + this._content_url;
			return ret;
		}
		
		// ----------------------------------------------------------
		public function set content_url(url:String):void{
			this._content_url = url;
			this.setWorkSheet();
		}
		
		/**
		 * WorkSheetを読み込んでセットする
		 **/
		public function setWorkSheet():void{
			this.isBusy(true);
			
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, this.onResponseGetWorkSheet);
			
			var req:URLRequest = new URLRequest();
			req.url = this._content_url;
			req.method = URLRequestMethod.GET;
			
			req.requestHeaders.push(new URLRequestHeader('Authorization', 'GoogleLogin auth=' + this.spreadsheet.Auth.auth));
			req.requestHeaders.push(new URLRequestHeader('Content-Type','application/atom+xml'));			//req.requestHeaders.push(new URLRequestHeader('GData-Version', '2'));
			loader.load(req);
		}
		
		private function onResponseGetWorkSheet(e:Event):void{
			var loader:URLLoader = e.currentTarget as URLLoader;
			loader.removeEventListener(Event.COMPLETE, this.onResponseGetWorkSheet);
			
			var res:XML = new XML(loader.data);
			
			var atom:Namespace = res.namespace();
			var openSearch:Namespace = res.namespace('openSearch');
			var gs:Namespace = res.namespace('gs');
			
			//trace(res);
			
			var worksheets:Array = new Array();
			for each (var entry:XML in res.atom::entry) {
				var worksheet:WorkSheet = this.parseToWorkSheet(entry);
				worksheets.push(worksheet);
			}
			this.WorkSheets = new ArrayCollection(worksheets);
			this.isBusy(false);
		}
		
		private function parseToWorkSheet(entry:XML):WorkSheet{
			var atom:Namespace = entry.namespace();
			var gs:Namespace = entry.namespace('gs');
			
			var work_sheet:WorkSheet = new WorkSheet();
			
			work_sheet.sheet = this;
			
			work_sheet.id = entry.atom::id;
			work_sheet.updated = DateUtil.parseW3CDTF(entry.atom::updated);
			work_sheet.title = entry.atom::title;
			work_sheet.rowCount = entry.gs::rowCount;
			work_sheet.colCount = entry.gs::colCount;
			
			var links:XMLList = entry.atom::link;
			for each (var link:XML in entry.atom::link) {
				if(link.@rel == "http://schemas.google.com/spreadsheets/2006#listfeed"){
					work_sheet.list_url = link.@href;
				}else if(link.@rel == "http://schemas.google.com/spreadsheets/2006#cellsfeed"){
					
				}else if(link.@rel == "edit"){
					
				}
			}
				
			trace(work_sheet);
			return work_sheet;
		}

	}
}